package net.schmurgon.gwt.ui;

import com.google.gwt.app.place.AbstractRecordEditActivity;
import com.google.gwt.app.place.PlaceController;
import com.google.gwt.app.place.RecordEditView;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.RecordRequest;
import com.google.gwt.requestfactory.shared.RequestObject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.valuestore.shared.SyncResult;
import com.google.gwt.valuestore.shared.Value;
import net.schmurgon.gwt.request.RunRecord;
import net.schmurgon.gwt.shared.CourseType;
import net.schmurgon.gwt.request.RunnerRecord;
import net.schmurgon.gwt.request.ApplicationRequestFactory;
import net.schmurgon.gwt.scaffold.generated.RunEditView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * An activity that requests all info on an Run, allows the user to edit
 * it, and persists the results.
 */
public class RunEditActivity extends AbstractRecordEditActivity<RunRecord> {
	private static RecordEditView<RunRecord> defaultView;

	private static RecordEditView<RunRecord> getDefaultView() {
		if (defaultView == null) {
			defaultView = new RunEditView();
		}
		return defaultView;
	}

	private final ApplicationRequestFactory requests;

	/**
	 * Creates an activity that uses the default singleton view instance.
	 */
	public RunEditActivity(RunRecord proxy, ApplicationRequestFactory requests, PlaceController placeController, boolean creating) {
		this(getDefaultView(), proxy, requests, placeController, creating);
	}

	/**
	 * Creates an activity that uses its own view instance.
	 */
	public RunEditActivity(RecordEditView<RunRecord> view, RunRecord proxy, ApplicationRequestFactory requests, PlaceController placeController, boolean creating) {
		super(view, proxy, RunRecord.class, creating, requests, placeController);
		this.requests = requests;
	}

    @Override
    public void start(Display display, EventBus eventBus) {
    
    getRunEditView().setCourseTypePickerValues(Arrays.asList(CourseType.values()));

    
    
    getRunEditView().setRunnerPickerValues(Collections.<RunnerRecord> emptyList());

    requests.runnerRequest().findRunnerEntries(0, 50).with(
        net.schmurgon.gwt.scaffold.generated.RunnerRecordRenderer.instance().getPaths()).fire(
            new Receiver<List<RunnerRecord>>() {
                public void onSuccess(List<RunnerRecord> response,
                    Set<SyncResult> syncResults) {
                    List<RunnerRecord> values = new ArrayList<RunnerRecord>();
                    values.add(null);
                    values.addAll(response);
                    getRunEditView().setRunnerPickerValues(values);
            }
    });
    
        super.start(display, eventBus);
    }


	@Override
	protected RecordRequest<RunRecord> getFindRequest(Value<Long> id) {
	    return requests.runRequest().findRun(id);
	}

	protected RequestObject<Void> getPersistRequest(RunRecord record) {
	    return requests.runRequest().persist(record);
	}

    private RunEditView getRunEditView() {
        return ((RunEditView) getView());
    }
}
